package maple.thelittleredbook.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.extern.slf4j.Slf4j;
import maple.thelittleredbook.common.dto.ApiResponse;
import maple.thelittleredbook.common.exception.BusinessException;
import maple.thelittleredbook.common.service.AuthService;
import maple.thelittleredbook.logging.annotation.SystemLog;
import maple.thelittleredbook.user.dto.UserDTO;
import maple.thelittleredbook.user.entity.User;
import maple.thelittleredbook.user.service.RoleService;
import maple.thelittleredbook.user.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/admin")
public class PermissionController {

    private final UserService userService;
    private final AuthService authService;
    private final RoleService roleService;

    public PermissionController(AuthService authService, UserService userService, RoleService roleService) {
        this.authService = authService;
        this.userService = userService;
        this.roleService = roleService;
    }

    @GetMapping("test_USER")
    @PreAuthorize("hasAnyRole('USER')")
    @SystemLog(operation = "权限测试")
    public ResponseEntity<ApiResponse<String>> testUser() throws Exception {
        String Role = authService.getCurrentUserRole();
        return ResponseEntity.ok(ApiResponse.success("success", Role));
    }

    @GetMapping("test_ADMIN")
    @PreAuthorize("hasAnyRole('ADMIN')")
    @SystemLog(operation = "权限测试")
    public ResponseEntity<ApiResponse<String>> testAdmin() throws Exception {
        String Role = authService.getCurrentUserRole();
        return ResponseEntity.ok(ApiResponse.success("success", Role));
    }

    @GetMapping("test_SUPERADMIN")
    @PreAuthorize("hasAnyRole('SUPERADMIN')")
    @SystemLog(operation = "权限测试")
    public ResponseEntity<ApiResponse<String>> testSuperAdmin() throws Exception {
        String Role = authService.getCurrentUserRole();
        return ResponseEntity.ok(ApiResponse.success("success", Role));
    }

    /**
     * 分页获取用户列表
     *
     * @param pageSize
     * @param pageNum
     * @return
     * @throws Exception
     */
    @GetMapping("users")
    @PreAuthorize("hasAnyRole('ADMIN')")
    @SystemLog(operation = "获取用户列表")
    public ResponseEntity<ApiResponse<Page<User>>> users(
            @Min(value = 1, message = "页码最小为1")
            @RequestParam Integer pageSize,
            @Min(value = 1, message = "每页数量最小为1")
            @Max(value = 100, message = "每页数量最大为100")
            @RequestParam Integer pageNum) throws Exception {
        String Role = authService.getCurrentUserRole();


        return roleService.GetUsers(pageSize, pageNum);
    }

    /**
     * 启用用户
     *
     * @param userId 启用用户的ID
     * @return
     * @throws BusinessException
     */

    @PostMapping("user/activate")
    @PreAuthorize("hasAnyRole('ADMIN')")
    @SystemLog(operation = "用户启用")
    public ResponseEntity<ApiResponse<String>> activate(@RequestParam Long userId) throws BusinessException {


        return roleService.EnableAccount(userId);
    }

    /**
     * 封禁用户
     *
     * @param userId 封禁用户的ID
     * @return
     * @throws BusinessException
     */
    @PostMapping("user/deactivate")
    @PreAuthorize("hasAnyRole('ADMIN')")
    @SystemLog(operation = "用户封禁")
    public ResponseEntity<ApiResponse<String>> deactivate(@RequestParam Long userId) throws BusinessException {


        return roleService.DisableAccount(userId);
    }

    /**
     * 修改用户权限
     *
     * @param userId  修改目标用户
     * @param newRole 用户新的权限
     * @return 是否成功
     * @throws BusinessException
     */
    @PostMapping("user/role_update")
    @PreAuthorize("hasAnyRole('SUPERADMIN')")
    @SystemLog(operation = "用户权限修改")
    public ResponseEntity<ApiResponse<String>> roleUpdate(@RequestParam Long userId, @RequestParam String newRole) throws BusinessException {


        return roleService.UpdateAccountRole(userId, newRole);
    }


}
